/* 
 * File:   main.cpp
 * Author: sergio
 *
 * Created on 3 de septiembre de 2008, 18:06
 */

#include <stdio.h>

#include "GameControl.h"
#include <SDL/SDL_video.h>

#include "Sprite.h"
#include <SDL/SDL_rotozoom.h>
#include <SDL/SDL.h>             // Incluimos la librería SDL
#include <SDL/SDL_gfxPrimitives.h>
#include <SDL/SDL_framerate.h>

#include "Sprite.h"
#include "ResourceManager.h"
#include "ConfigurationFile.h"
#include "InputHandler.h"
#include "AnimatedSprite.h"
#include "LevelEditor.h"
#include "SpriteManager.h"

int main(int argc, char** argv) {
	
	LevelEditor editor; editor.run(); return 0; ///---RUN EDITOR---///

    SDL_Surface *pantalla; // Definimos una superficie
    SDL_Surface *imagen1, *imagen2;
    SDL_Event evento; // Definimos una variable de eventos jare
	SpriteManager* spr_manager = SpriteManager::getInstance();
	FPSmanager framerate_manager;

    // Inicializamos SDL
    if (SDL_Init(SDL_INIT_EVERYTHING /*SDL_INIT_VIDEO*/ ) < 0) {
        // En caso de error
        fprintf(stderr, "SDL couldn't be initialised: %s\n", SDL_GetError());
        exit(1);
    }
	
    atexit(SDL_Quit); // Al salir, cierra SDL

    // Establecemos el modo de pantalla
//    pantalla = SDL_SetVideoMode(640, 480, 32, SDL_ANYFORMAT | SDL_DOUBLEBUF | SDL_FULLSCREEN);
    pantalla = SDL_SetVideoMode(640, 480, 32, SDL_ANYFORMAT | SDL_DOUBLEBUF);// | SDL_FULLSCREEN);

    if (pantalla == NULL) {
        // Si no hemos podido inicializar la superficie
        fprintf(stderr, "Error: main surface couldn't be created: %s\n", SDL_GetError());
        exit(1);
    }
	
    // Personalizamos el título de la ventana
    SDL_WM_SetCaption("Bomberman Project - By Format C 2008", NULL);
    
	
	//FPS manager initialization
	SDL_initFramerate(&framerate_manager);
	SDL_setFramerate(&framerate_manager, 30);
	
	/*ConfigurationFile prueba;
	prueba.loadFile("/home/sergio/prueba.txt");
	
	printf("match_time=%d\n", prueba.getInteger("match_time"));//, 0));
	printf("otracosa=%d\n", prueba.getInteger("otracosa"));
	
    //imagen1 = IMG_Load("C:/oni.png");
	imagen1 = resourceManager->loadImage("/home/sergio/oni.png");//IMG_Load("/home/sergio/oni.png");
	
	imagen2 = resourceManager->loadImage("/home/sergio/oni.png");
	
	resourceManager->unloadImage(imagen2);
	resourceManager->unloadImage(imagen1);*/
	
	stringRGBA(pantalla, 0, 0, "Dectectando dispositivos...", 255, 255, 255, 255);
	SDL_Flip(pantalla);

	gInputHandler->detectDevices();
	
	stringRGBA(pantalla, 0, 10, "Dectectados.", 255, 255, 255, 255);
	SDL_Flip(pantalla);

	DeviceButtonInfo dbi = {SDLK_ESCAPE, DEVICE_KEYBOARD, 0};
	//DeviceButtonInfo dbi = {8, DEVICE_PAD, 0};
	ButtonState* escape = gInputHandler->getLinkFromButtonInfo(dbi);
	//ButtonState* tecla = gInputHandler->getLinkFromPressedButton(dbi);

	/*stringRGBA(pantalla, 0, 20, "ARRIBA", 255, 255, 255, 255);
	SDL_Flip(pantalla);
	gInputHandler->getLinkFromPressedButton(dbi);
	GameControl::getGameControl(0)->setButtonInfo(BUTTON_UP, dbi);

	stringRGBA(pantalla, 0, 30, "ABAJO", 255, 255, 255, 255);
	SDL_Flip(pantalla);
	gInputHandler->getLinkFromPressedButton(dbi);
	GameControl::getGameControl(0)->setButtonInfo(BUTTON_DOWN, dbi);

	stringRGBA(pantalla, 0, 40, "IZQUIERDA", 255, 255, 255, 255);
	SDL_Flip(pantalla);
	gInputHandler->getLinkFromPressedButton(dbi);
	GameControl::getGameControl(0)->setButtonInfo(BUTTON_LEFT, dbi);

	stringRGBA(pantalla, 0, 50, "DERECHA", 255, 255, 255, 255);
	SDL_Flip(pantalla);
	gInputHandler->getLinkFromPressedButton(dbi);
	GameControl::getGameControl(0)->setButtonInfo(BUTTON_RIGHT, dbi);

	stringRGBA(pantalla, 0, 60, "FUEGO", 255, 255, 255, 255);
	SDL_Flip(pantalla);
	gInputHandler->getLinkFromPressedButton(dbi);
	GameControl::getGameControl(0)->setButtonInfo(BUTTON_FIRE, dbi);

	stringRGBA(pantalla, 0, 70, "ESPECIAL 1", 255, 255, 255, 255);
	SDL_Flip(pantalla);
	gInputHandler->getLinkFromPressedButton(dbi);
	GameControl::getGameControl(0)->setButtonInfo(BUTTON_SPECIAL1, dbi);

	stringRGBA(pantalla, 0, 80, "ESPECIAL 2", 255, 255, 255, 255);
	SDL_Flip(pantalla);
	gInputHandler->getLinkFromPressedButton(dbi);
	GameControl::getGameControl(0)->setButtonInfo(BUTTON_SPECIAL2, dbi);

	GameControl::saveGameControls("teclas.cfg");*/
	GameControl::loadGameControls("teclas.cfg");

	//imagen1 = gResourceManager->loadImage("c:/op.png");//IMG_Load("/home/sergio/oni.png");
	/*anim_spr = new AnimatedSprite();
//	anim_spr->addAnimation("andar","c:/Bomberman/Mario/Mario.txt");
	anim_spr->addAnimation("andar","Chars/Mario/walk.txt");
	anim_spr->setAnimation("andar");
	anim_spr->playAnimation();
	//anim_spr->setAlpha(128);
	anim_spr->setPosition(320, 240, 0);

	spr_manager->addForegroundSprite(anim_spr);*/

	Player* anim_spr = spr_manager->createPlayer("Mario");
	anim_spr->setPosition(320, 240, 0);
	anim_spr->playAnimation();
	anim_spr->setGameControl(GameControl::getGameControl(0));
	anim_spr->setMinSpeed();

	//anim_spr->rotate(33);
	//anim_spr->scale(5.0, 5.0);

	//SDL_SetColorKey(imagen1, SDL_SRCCOLORKEY, SDL_MapRGB(imagen1->format, 255, 255, 255)); 
	
	//Sprite *spr = new Sprite(imagen1);
	//spr->setHotPoint(0,0);
	//spr->setAlpha(128);
	
    // Bucle infinito
    for (;;) {
        // Consultamos los eventos
        /*while (SDL_PollEvent(&evento)) {
			
			if (evento.type == SDL_QUIT) // Si es de salida
                return 0;
			else if(evento.type == SDL_KEYDOWN){
				switch(evento.key.keysym.sym){
					case SDLK_ESCAPE:
						return 0;
					case SDLK_UP:
						spr->move(0, -10);
						break;
					case SDLK_DOWN:
						spr->move(0, 10);
						break;
					case SDLK_LEFT:
						spr->move(-10, 0);
						break;
					case SDLK_RIGHT:
						spr->move(10, 0);
						break;
					case SDLK_PAGEUP:
						spr->scale(1.5, 1.5);
						break;
					case SDLK_PAGEDOWN:
						spr->scale(1, 1);
						break;
					case SDLK_SPACE:
						spr->horizontalMirror(Sprite::MIRROR_SWITCH);
						break;
					case SDLK_RETURN:
						spr->verticalMirror(Sprite::MIRROR_SWITCH);
						break;
					case SDLK_HOME:
						spr->turn(30);
						break;
					case SDLK_END:
						spr->turn(-30);
						break;
				}
			}*/
		
		while(SDL_PollEvent(&evento)){
			if(evento.type == SDL_QUIT) exit(0);
		}

		gInputHandler->update();

		if(escape->button_down) break;
		//if(tecla->button_held) anim_spr->move(1,0,0);
		/*if(GameControl::getGameControl(0)->getButtonState(BUTTON_LEFT)->button_held) anim_spr->move(-2,0,0);
		if(GameControl::getGameControl(0)->getButtonState(BUTTON_RIGHT)->button_held) anim_spr->move(2,0,0);
		if(GameControl::getGameControl(0)->getButtonState(BUTTON_UP)->button_held) anim_spr->move(0,-2,0);
		if(GameControl::getGameControl(0)->getButtonState(BUTTON_DOWN)->button_held) anim_spr->move(0,2,0);*/
		anim_spr->updateInput();
		anim_spr->update();

		//Limpiamos la pantalla
		SDL_FillRect(pantalla, NULL, SDL_MapRGB(pantalla->format, 255, 255, 255));
		stringRGBA(pantalla, 0, 0, "Prueba con teclas cargadas de una configuracion", 255, 0, 0, 255);

		//spr->draw(pantalla);
		/*anim_spr->updateAnimation();
		anim_spr->draw(pantalla);*/
		spr_manager->drawAll(pantalla);

		SDL_Flip(pantalla);
		//}
		//At the end of the loop the desired delay is introduced
		SDL_framerateDelay(&framerate_manager);
    }
	
	gInputHandler->destroy();
	return 0;

}
